package com.dhcc.sds.busi.controller;


import java.util.Date;
import java.util.List;
import java.util.Map;


import com.dhcc.core.framework.security.ShiroKit;
import com.dhcc.core.modules.system.entity.User;
import com.dhcc.sds.busi.entity.SdsQcincaseEntity;
import com.dhcc.sds.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.base.warpper.EmptyWarpper;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.exception.BizExceptionEnum;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.core.framework.util.CommonUtil;



import com.dhcc.sds.busi.entity.SdsQcincaseOperlogEntity;
import com.dhcc.sds.busi.service.ISdsQcincaseOperlogService;


/**
 * 病例操作日志
 * 
 * @ClassName: SdsQcincaseOperlogController
 * @Description: TODO
 * @author sunyingjie
 * @date 2021-03-22 10:09:14
 */
@Controller
@RequestMapping("${sys.backendPath}/sds/sdsqcincaseoperlog")
public class SdsQcincaseOperlogController extends BaseController {

	private String PREFIX = "/backend/sds/sdsqcincaseoperlog/";

	@Autowired
	private ISdsQcincaseOperlogService sdsQcincaseOperlogService;
	

    /**
     * 跳转到病例操作日志列表首页
     */
    @RequestMapping("{mrListId}")
    public String index(@PathVariable Long mrListId,Model model) {
        model.addAttribute("mrListId", mrListId);
        return PREFIX + "sdsqcincaseoperlog.html";
    }

    /**
     * 获取病例操作日志列表
     */
	@SuppressWarnings("unchecked")
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam Map<String, Object> params) {
		Page<SdsQcincaseOperlogEntity> page = new PageFactory<SdsQcincaseOperlogEntity>().defaultPage();
        List<SdsQcincaseOperlogEntity> result = this.sdsQcincaseOperlogService.page(page,params);
        page.setRecords((List<SdsQcincaseOperlogEntity>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }

    /**
     * 增加病例操作日志
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public void add(@RequestParam Map<String, Object> params){
        String execUser;
        if (CommonUtil.isNotEmpty(params.get("execUser"))) {
            execUser = params.get("execUser").toString();
        } else {
            User user = ShiroKit.getUser();
            execUser = user.getLoginName();
        }
        Date execDate = new Date();
        Long mrListId  = Long.valueOf(params.get("mrListId").toString());
        String stateCode  = params.get("status").toString();
        String operStartTime = "";
        String operEndTime = "";
        if (params.get("operStartTime")!=null){
            operStartTime = params.get("operStartTime").toString();
        }
        if (params.get("operEndTime")!=null){
            operEndTime = params.get("operEndTime").toString();
            execDate = DateUtil.parseTime(operEndTime);
        }
        // 增加病例操作日志
        SdsQcincaseOperlogEntity sdsQcincaseOperlogEntity = new SdsQcincaseOperlogEntity();
        sdsQcincaseOperlogEntity.setInCaseId(mrListId);
        sdsQcincaseOperlogEntity.setOperState(stateCode);
        if (params.get("reason")!=null) {
            sdsQcincaseOperlogEntity.setOperReason(String.valueOf(params.get("reason")));
        }
        if (params.get("resume")!=null) {
            sdsQcincaseOperlogEntity.setOperResume(String.valueOf(params.get("resume")));
        }
        sdsQcincaseOperlogEntity.setOperUser(execUser);
        sdsQcincaseOperlogEntity.setOperDate(execDate);
        if (CommonUtil.isNotEmpty(operStartTime)){
            Date date = DateUtil.parseTime(operStartTime);
            sdsQcincaseOperlogEntity.setOperStartDate(date);
        }
        sdsQcincaseOperlogEntity.insert();
    }
}
